//
//  NSArray+STTools_ArrayTool.m
//  togetherPlay
//
//  Created by coder on 2018/12/27.
//  Copyright © 2018 stoneobs.qq.com. All rights reserved.
//

#import "NSArray+STTools_ArrayTool.h"

@implementation NSArray (STTools_ArrayTool)
- (NSArray*)stoneobs_fun_arrayFromObjKeyName:(NSString*)KeyName{
    if (![KeyName containsString:@"obj."]) {
        NSLog(@"检索格式不正确 需要obj.开头");
        return nil;
    }else{
        NSMutableArray * stoneobs_temp_dealArray = NSMutableArray.new;
        NSArray * stoneobs_temp_allArray = [KeyName componentsSeparatedByString:@"."];
        NSArray * stoneobs_temp_menuArray = [stoneobs_temp_allArray subarrayWithRange:NSMakeRange(1, stoneobs_temp_allArray.count-1)];
        
        for (NSObject * object in self) {
            id value;
            for (NSString * objKey in stoneobs_temp_menuArray) {
                value = [object valueForKey:objKey];
            }
            //如果存在 且 不是基本数据类型
            if (![value isKindOfClass:NSNumber.class]) {
                [stoneobs_temp_dealArray addObject:value];
            }else{
                //如果存在 且 是基本数据类型
                NSString * dValue = [NSString stringWithFormat:@"%@",value];
                [stoneobs_temp_dealArray addObject:dValue];
            }
            
            
        }
        return stoneobs_temp_dealArray;
        
    }
    return nil;
}
- (NSArray*)stoneobs_fun_arrayFromObjKeyName:(NSString*)KeyName objValue:(id)objValue{
    if (![KeyName containsString:@"obj."]) {
        NSLog(@"检索格式不正确 需要obj.开头");
        return nil;
    }else{
        NSMutableArray * stoneobs_temp_dealArray = NSMutableArray.new;
        NSArray * stoneobs_temp_allArray = [KeyName componentsSeparatedByString:@"."];
        NSArray * stoneobs_temp_menuArray = [stoneobs_temp_allArray subarrayWithRange:NSMakeRange(1, stoneobs_temp_allArray.count-1)];
        
        for (NSObject * object in self) {
            id value;
            for (NSString * objKey in stoneobs_temp_menuArray) {
                value = [object valueForKey:objKey];
      
                
            }
            //如果存在 且 不是基本数据类型
            if (![value isKindOfClass:NSNumber.class]) {
                if (value == objValue) {
                    [stoneobs_temp_dealArray addObject:object];
                }else{
                    if ([value isEqualToString:objValue]) {
                        [stoneobs_temp_dealArray addObject:object];
                    }
                }
            }else{
                //如果存在 且 是基本数据类型
                NSString * dValue = [NSString stringWithFormat:@"%@",value];
                NSString * equalValue = [NSString stringWithFormat:@"%@",objValue];
                if ([dValue isEqualToString:equalValue]) {
                    [stoneobs_temp_dealArray addObject:object];
                }
            }
        }
        return stoneobs_temp_dealArray;
        
    }
    return nil;
}
//解散数组 如果数组嵌套数组，全部解散 将模型 放到一个数组里面
- (NSArray*)stoneobs_fun_dissolveArray{
    NSMutableArray *  stoneobs_temp_dealArray = NSMutableArray.new;
    for (NSArray * array in self) {
        if ([array isKindOfClass:NSArray.class]) {
            //解散这个数组
            NSArray * digui = [self stoneobs_fun_dissolveArrayWithArray:array];
            [stoneobs_temp_dealArray addObjectsFromArray:digui];
        }else{
            [stoneobs_temp_dealArray addObject:array];
        }
    }
    return stoneobs_temp_dealArray;
}
- (NSArray*)stoneobs_fun_dissolveArrayWithArray:(NSArray*)inputArray{
    NSMutableArray *  stoneobs_temp_dealArray = NSMutableArray.new;
    for (NSArray * array in inputArray) {
        if ([array isKindOfClass:NSArray.class]) {
            //解散这个数组
            NSArray * digui = [self stoneobs_fun_dissolveArrayWithArray:array];
            [stoneobs_temp_dealArray addObject:digui];
        }else{
            [stoneobs_temp_dealArray addObject:array];
        }
    }
    return stoneobs_temp_dealArray;
}
- (NSArray *)stoneobs_fun_ascendingArrayFromObjKeyName:(NSString *)KeyName{
    NSSortDescriptor *stoneobs_temp_des1 = [NSSortDescriptor sortDescriptorWithKey:KeyName ascending:YES];
    NSArray* stoneobs_temp_dealArray = [self sortedArrayUsingDescriptors:@[stoneobs_temp_des1]];
    return stoneobs_temp_dealArray;
}
- (NSArray *)stoneobs_fun_descendingArrayFromObjKeyName:(NSString *)KeyName{
    NSSortDescriptor *stoneobs_temp_des1 = [NSSortDescriptor sortDescriptorWithKey:KeyName ascending:NO];
    NSArray* stoneobs_temp_dealArray = [self sortedArrayUsingDescriptors:@[stoneobs_temp_des1]];
    return stoneobs_temp_dealArray;
}
+ (NSArray *)stoneobs_fun_safeArrayWithValue:(id)value{
    NSArray * stoneobs_temp_array = value;
    if ([stoneobs_temp_array isKindOfClass:NSDictionary.class]) {
        NSDictionary * dic = (id)value;
        stoneobs_temp_array = dic.allValues;
    }
    return stoneobs_temp_array;
}

- (id)stoneobs_fun_randomObject{
    if (self.count) {
        return self[arc4random_uniform((u_int32_t)self.count)];
    }
    return nil;
}
- (NSMutableArray*)stoneobs_fun_getRandomSortArr;
{
    NSArray * arr = self.copy;
    NSMutableArray *newArr = [NSMutableArray new];
    while (newArr.count != arr.count) {
        //生成随机数
        int x =arc4random() % arr.count;
        id obj = arr[x];
        if (![newArr containsObject:obj]) {
            [newArr addObject:obj];
        }
    }
    return newArr;
}
- (NSMutableArray*)stoneobs_fun_getRandomSortArrWith_count:(NSInteger)count;
{
    NSArray * arr = self.copy;
    NSMutableArray *newArr = [NSMutableArray new];
    while (newArr.count != count) {
        //生成随机数
        int x =arc4random() % arr.count;
        id obj = arr[x];
        if (![newArr containsObject:obj]) {
            [newArr addObject:obj];
        }
    }
    return newArr;
}
@end
